package com.ujiuye.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ujiuye.config.MessageConstant;
import com.ujiuye.config.PageResult;
import com.ujiuye.config.QueryPageBean;
import com.ujiuye.config.Result;
import com.ujiuye.mapper.CheckgroupCheckitemMapper;
import com.ujiuye.mapper.CheckgroupMapper;
import com.ujiuye.pojo.Checkgroup;
import com.ujiuye.pojo.CheckgroupCheckitem;
import com.ujiuye.service.ICheckgroupService;
import com.ujiuye.service.ICheckitemService;
import com.ujiuye.vo.CheckgroupCheckitemVo;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 检查组 服务实现类
 * </p>
 *
 * @author zs
 * @since 2021-07-05
 */
@Service
public class CheckgroupServiceImpl extends ServiceImpl<CheckgroupMapper, Checkgroup> implements ICheckgroupService {
    @Resource
    private CheckgroupMapper checkgroupMapper;
    @Resource
    private CheckgroupCheckitemMapper checkgroupCheckitemMapper;

    @Override
    public PageResult PageList(QueryPageBean queryPageBean) {
        Page<Checkgroup> page = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        String queryString = queryPageBean.getQueryString();
        QueryWrapper<Checkgroup> queryWrapper = new QueryWrapper();
        if(queryString!=null && queryString.length()>0){
            queryWrapper.eq("code",queryString)
                    .or().eq("name",queryString)
                    .or().eq("helpcode",queryString);
        }
        Page<Checkgroup> checkgroupPage = checkgroupMapper.selectPage(page, queryWrapper);
        PageResult pageResult = new PageResult(checkgroupPage.getTotal(),checkgroupPage.getRecords());
        return pageResult;
    }

    @Override
    public void save(Integer[] ids, Checkgroup checkgroup) {
         checkgroupMapper.insert(checkgroup);
         //添加中间表
        for (Integer id : ids) {
            CheckgroupCheckitem checkgroupCheckitem = new CheckgroupCheckitem();
            //添加检查组返回主键值
            checkgroupCheckitem.setCheckgroupId(checkgroup.getId());
            checkgroupCheckitem.setCheckitemId(id);
            checkgroupCheckitemMapper.insert(checkgroupCheckitem);
        }
    }

    @Override
    public Result getById(int id) {
        //通过ID查询检查组
        Checkgroup checkgroup = checkgroupMapper.selectById(id);
        //构造条件对象
        QueryWrapper<CheckgroupCheckitem> queryWrapper = new QueryWrapper();
        //通过检查组的ID去查询检查组和检查项的中间表
        queryWrapper.eq("checkgroup_id",id);
        List<CheckgroupCheckitem> checkgroupCheckitems = checkgroupCheckitemMapper.selectList(queryWrapper);
        //创建一个集合来装检查项的ID
        List<Integer> checkitemIds = new ArrayList<>();
        //遍历从中间表查回来的中间表对象在将检查项的ID放入集合中
        for (CheckgroupCheckitem checkgroupCheckitem : checkgroupCheckitems) {
            checkitemIds.add(checkgroupCheckitem.getCheckitemId());
        }
        //将要返回给前端的数据存入一个vo对象中
        CheckgroupCheckitemVo checkgroupCheckitemVo = new CheckgroupCheckitemVo(checkgroup,checkitemIds);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkgroupCheckitemVo);
    }

    @Override
    public void update(Integer[] ids, Checkgroup checkgroup) {
        //修改信息
        checkgroupMapper.updateById(checkgroup);
        //构造条件先将要修改的删除在重新添加
        QueryWrapper<CheckgroupCheckitem> queryWrapper = new QueryWrapper();
        queryWrapper.eq("checkgroup_id",checkgroup.getId());
        checkgroupCheckitemMapper.delete(queryWrapper);
        for (Integer id : ids) {
            CheckgroupCheckitem checkgroupCheckitem = new CheckgroupCheckitem(checkgroup.getId(),id);
            checkgroupCheckitemMapper.insert(checkgroupCheckitem);
        }
    }
}
